﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Dia2Lib;

namespace Kokomo.Mirror.PeFormat.Symbols
{
	class SymbolEnumerator : SymbolBase, IEnumerator<Symbol>
	{
		internal SymbolEnumerator(
			IDiaEnumSymbols diaSymbolEnum,
			SymbolSource source
			)
			: base(source)
		{
			this._diaSymbolEnum = new ComWrapper<IDiaEnumSymbols>(diaSymbolEnum);
		}

		private ComWrapper<IDiaEnumSymbols> _diaSymbolEnum;
		private IDiaEnumSymbols DiaSymbolEnum { get { return this._diaSymbolEnum.Object; } }

		protected Symbol CreateObject(IDiaSymbol comObject)
		{
			return Symbol.FromDiaSymbol(comObject, this.Source);
		}


		private Symbol _current;
		public Symbol Current
		{
			get { return this._current; }
		}

		public void Dispose()
		{
		}

		object System.Collections.IEnumerator.Current
		{
			get { return this._current; }
		}

		public bool MoveNext()
		{
			IDiaSymbol obj;
			uint fetched;
			this.DiaSymbolEnum.Next(1, out obj, out fetched);
			if (fetched != 1)
			{
				this._current = null;
				return false;
			}

			this._current = this.CreateObject((IDiaSymbol)obj);
			return true;
		}

		public void Reset()
		{
			this.DiaSymbolEnum.Reset();
		}
	}
}
